package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.domain.pojo.WfActionInfo;
import com.yu.crm.domain.pojo.WfNodeInfo;
import com.yu.crm.domain.pojo.WfNodeRole;
import com.yu.crm.domain.request.NodeRoleUserReq;
import com.yu.crm.domain.response.WfNodeInfoListReq;
import com.yu.crm.mapper.WfNodeInfoMapper;
import com.yu.crm.service.WfActionInfoService;
import com.yu.crm.service.WfNodeInfoService;
import com.yu.crm.service.WfNodeRoleService;
import com.yxzz.common.exception.GeneralException;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.DateHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 工作流节点表 服务实现类
 * </p>
 *
 * @author donnie-bin
 * @since 2019-10-08
 */
@Service
public class WfNodeInfoServiceImpl extends ServiceImpl<WfNodeInfoMapper, WfNodeInfo> implements WfNodeInfoService {

    @Autowired
    private WfNodeRoleService wfNodeRoleService;
    @Autowired
    private WfActionInfoService wfActionInfoService;


    /**
     * 通过应用id查询节点集合
     * @param appId
     * @return
     */
    @Override
    public List<WfNodeInfo> nodeInfoListByAppId(String appId) {
        //return nodeInfoListByAppId02(appId);
        QueryWrapper<WfNodeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id",appId);
        queryWrapper.eq("is_deleted",0);
        List<WfNodeInfo> wfNodeInfos = baseMapper.selectList(queryWrapper);
        if (DataHelpers.isNotEmpty(wfNodeInfos) && wfNodeInfos.size()>0) {
            for (WfNodeInfo wfNodeInfo:wfNodeInfos
            ) {
                List<WfActionInfo> list = wfActionInfoService.listByPid(appId,wfNodeInfo.getId().toString());
                wfNodeInfo.setActionInfoList(list);
            }
        }
        return wfNodeInfos;
    }

    @Override
    public String getRoot(String appId) {
        try {
            QueryWrapper<WfNodeInfo> queryWrapper = new QueryWrapper();
            queryWrapper.eq("app_id",appId);
            queryWrapper.eq("is_deleted",0);
            queryWrapper.eq("is_root",1);
            WfNodeInfo wfNodeInfo = baseMapper.selectOne(queryWrapper);
            if (DataHelpers.isEmpty(wfNodeInfo)) {
                throw new GeneralException("暂无根节点");
            }
            return wfNodeInfo.getId();
        }catch (Exception e) {
            throw new GeneralException("工作流根节点配置错误");
        }
    }

    /**
     * 节点信息保存
     * @param req
     */
    @Override
    public List<WfNodeInfo> saveNodeList(WfNodeInfoListReq req) {
        //先做删除后做新增
        String appId = req.getAppId();
        //节点信息删除
        del(appId);
        //节点角色人员删除
        wfNodeRoleService.del(appId);

        List<WfNodeInfo> nodeInfoList = req.getNodeInfoList();
        if (DataHelpers.isNotEmpty(nodeInfoList) && nodeInfoList.size()>0) {
            for (WfNodeInfo w:nodeInfoList
                 ) {
                w.setId(w.getNodeId());
                w.setAppId(appId);
                w.setGmtCreate(DateHelpers.now());
                w.setIsDeleted(0);
                baseMapper.insert(w);

                //保存的该节点的全部人员
                if (DataHelpers.isNotEmpty(w.getIsAll()) && w.getIsAll().toString().equals("1")) {
                    WfNodeRole wfNodeRole = new WfNodeRole();
                    wfNodeRole.setAppId(appId);
                    wfNodeRole.setIsAll(1);
                    wfNodeRole.setGmtCreate(DateHelpers.now());
                    wfNodeRole.setIsDeleted(0);
                    wfNodeRole.setNodeId(w.getId());
                    wfNodeRoleService.save(wfNodeRole);
                }

                List<NodeRoleUserReq> nodeRoleUserReqList = w.getNodeRoleUserReqList();
                wfNodeRoleService.saveNodeRole(w.getId(),nodeRoleUserReqList,appId);
            }
        }
        return nodeInfoList;
    }

    /**
     * 通过应用id做删除
     * @param appId
     */
    public void del(String appId) {
        QueryWrapper<WfNodeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("app_id",appId);
        baseMapper.delete(queryWrapper);
    }

}
